<!-- subpkg_consult/department/index.vue -->
<script lang="ts" setup>
import { getDepAllAPI } from '@/api/consult'
import type { GetDepAllItemRes } from '@/types/consult'
import { computed, ref } from 'vue'

// 1. 获取路由参数
const props = defineProps<{ type: string; illnessType: string }>()

// 2. 查询科室
const depList = ref<GetDepAllItemRes[]>([])

const getDepList = async () => {
  const res = await getDepAllAPI()
  depList.value = res
}
getDepList()

// 3. 点击高亮
const activeIndex = ref(0)
// 4. 二级科室
const secondaryDepList = computed(() => {
  // 注意一定要加 ?. 因为一开始科室还没请求回来
  return depList.value[activeIndex.value]?.child
})
</script>

<template>
  <view class="department-page">
    <scroll-view scroll-y class="department-primary">
      <view
        v-for="(item, index) in depList"
        :key="item.id"
        class="department-item"
        :class="{ active: activeIndex === index }"
        @click="activeIndex = index"
      >
        {{ item.name }}
      </view>
    </scroll-view>
    <scroll-view class="department-secondary">
      <navigator
        v-for="item in secondaryDepList"
        :key="item.id"
        hover-class="none"
        :url="`/subpkg_consult/description/description?type=${props.type}&illnessType=${props.illnessType}&depId=${item.id}`"
        class="department-item"
      >
        {{ item.name }}
      </navigator>
    </scroll-view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
